package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.Laboratory;
import com.cdck.safe.dete.model.entity.Seat;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.query.BaseQuery;
import com.cdck.safe.dete.model.vo.LabVO;
import com.cdck.safe.dete.model.vo.SeatVO;
import com.cdck.safe.dete.service.LaboratoryService;
import com.cdck.safe.dete.service.SeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Laboratory)表控制层
 *
 * @author wp
 * @since 2024-02-25 15:40:15
 */
@Api(value = "laboratory")
@RestController
@RequestMapping("laboratory")
@RequiredArgsConstructor
public class LaboratoryController {

    private final LaboratoryService laboratoryService;

    @Resource
    private SeatService seatService;
    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams", value = "查询条件")
    })
    public PageResult<Laboratory> queryByPage(BaseKeywordsQuery queryParams) {
        IPage<Laboratory> result = laboratoryService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<Laboratory> getLaboratoryForm(
            @PathVariable Long id
    ) {
        Laboratory formData = laboratoryService.getLaboratoryFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param laboratory 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
            @RequestBody @Valid
                    Laboratory laboratory
    ) {
        boolean result = this.laboratoryService.saveLaboratory(laboratory);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateLaboratory(
            @PathVariable Long id,
            @RequestBody @Validated Laboratory laboratory) {
        boolean result = laboratoryService.updateLaboratory(id, laboratory);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteLaboratorys(
            @PathVariable String ids
    ) {
        boolean result = laboratoryService.deleteLaboratorys(ids);
        return Result.judge(result);
    }

    @ApiOperation(value = "下拉选择数据")
    @GetMapping("/list")
    public Result list() {
        List<Laboratory> list = laboratoryService.list();
        return Result.success(list);
    }
    @ApiOperation(value = "下拉联级查询")
    @GetMapping("/cascader")
    public Result cascader() {
        List<Laboratory> list = laboratoryService.list();
        List<LabVO> labVOList = list.stream().map(item -> {
            LabVO vo = new LabVO();
            vo.setValue(item.getId());
            vo.setLabel(item.getName());
            LambdaQueryWrapper<Seat> seatWrapper = new LambdaQueryWrapper<Seat>();
            seatWrapper.eq(Seat::getLabId, item.getId());
            seatWrapper.eq(Seat::getStatus, 0);
            List<Seat> seatList = seatService.list(seatWrapper);
            vo.setChildren(seatList.stream().map(seat -> {
                SeatVO seatVO = new SeatVO();
                seatVO.setValue(seat.getId());
                seatVO.setLabel(seat.getSeatNo());
                return seatVO;
            }).collect(Collectors.toList()));
            return vo;
        }).collect(Collectors.toList());
        return Result.success(labVOList);
    }


}

